package academic.nantes.teamsync;


import java.util.ArrayList;

import academic.nantes.teamsync.DAO.TeamSyncProject;
import academic.nantes.teamsync.DAO.TeamSyncTache;
import academic.nantes.teamsync.DAO.TeamSyncUser;
import academic.nantes.teamsync.customwidget.TeamSyncTable;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ProgressBar;
import android.widget.TableRow;
import android.widget.TextView;

public class SuiviProjet extends Activity {

	TeamSyncTable table;
	TableRow row;
	academic.nantes.teamsync.customwidget.TeamSyncTextView tv1;
	academic.nantes.teamsync.customwidget.TeamSyncTextView tv2;
	academic.nantes.teamsync.customwidget.TeamSyncTextView tv3;
	academic.nantes.teamsync.customwidget.TeamSyncTextView tv4;
	academic.nantes.teamsync.customwidget.TeamSyncTextView tv5;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.suiviprojet);

	}

	@Override
	protected void onResume(){
		super.onResume();
		repaintProgress();
		//travaux en cours
		loadGridTaches();

	}


	private ArrayList<TeamSyncTache> getMesTaches(){

		TeamSyncProject monProj = TeamSyncProject.getInstance();
		ArrayList<TeamSyncTache> myList = new ArrayList<TeamSyncTache>();
		//on ne prend que les EC
		for (Integer maTache : monProj.getRealisations().getTaches(TeamSyncUser.getInstance().getUid())) {
			TeamSyncTache t = monProj.getTacheById(maTache.intValue());
			if (null != t){
				myList.add(t);
			}
		}
		return myList;
	}
	private void loadGridTaches() {
		// TODO Auto-generated method stub

		table = (TeamSyncTable) findViewById(R.id.tableMesTaches);
		
		ArrayList<TeamSyncTache> myList = getMesTaches();
		
		if (myList.size() == 0){
			table.addEmptyRow(2);
		}
		else{
			for (TeamSyncTache t : myList) {

	        	row = new TableRow(this);
	            row.setOrientation(TableRow.VERTICAL);

	            tv1 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
	            tv2 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
	            tv3 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
	            tv4 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
	            tv5 = new academic.nantes.teamsync.customwidget.TeamSyncTextView(this);
	            		
	            tv1.setText(t.getLibelle());
	            tv2.setText(t.getDateDebTheoriqueToStringFormattedAs("dd/MM"));
	            tv3.setText(Float.toString(t.getDuree_reelle()) + " j.");
	            tv4.setText(t.getDateFinTh("dd/MM"));
	            tv5.setText(t.getEtat());

	            table.addcustomViewToRow(row, tv1);
	            table.addcustomViewToRow(row, tv2);
	            table.addcustomViewToRow(row, tv3);
	            table.addcustomViewToRow(row, tv4);
	            table.addcustomViewToRow(row, tv5);
	            
	            
			}
		}
	}



	/*
	 * repaint de la progressBar et de l'avancement
	 */
	private void repaintProgress() {
		ProgressBar pg = (ProgressBar)findViewById(R.id.pbContrib);
		final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
		ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
		String MyColor = "#4083BD";
		pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
		ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
		pg.setProgressDrawable(progress);   
		pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
		//MAJ de l'avancement
		int avancement = Math.round(getMesTaches().size() * 100 / TeamSyncProject.getInstance().getTaches().size());
		pg.setProgress(avancement);
		((TextView)findViewById(R.id.lblContrib)).setText(getString(R.string.lblContrib) + String.valueOf(avancement) + "%");
	}


}
